// pages/orderpage/orderpage.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: {
      "fault_type": app.data.ull +"wxSelectFault",
      "fault_info": app.data.ull +"wxSelectFaultinfo",
      "add_order": app.data.ull +"wxAddOrder"
    },
    fault_type: ({
    }),
    index: 0,
    info_index: 0,
    region: [],
    faultinfo: ({}),
    userId: 123,
    inputShow: 'none',
    selectShow: 'block',
    fault_type_input: "",
    orderId: "",
    address: {},
    tepvalue: "请选择地址",
    index_type:"",
  },
  //故障信息详情
  showFaultInfo: function () {
    var that = this;
    wx.request({
      url: that.data.url.fault_info,
      data: {
        faulttype: that.data.fault_type[that.data.index],
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        var a = [];
        var json = res.data;
        for (var j in json) {
          a.push(json[j].faultinfo);
        }
        that.setData({//如果在sucess直接写this就变成了wx.request()的this了.必须为getdata函数的this,不然无法重置调用函数
          faultinfo: a,
          inputShow: 'none'
        })
      },
      fail: function (res) { },
      complete: function (res) {
      },
    })
  },
  //故障类型选择
  bindpickerchange: function (e) {
    var that = this;
    that.setData({
      index: e.detail.value,
      info_index: 0
    })
    if (that.data.fault_type[that.data.index] == "其他") {
      that.setData({
        inputShow: 'block',
        faultinfo: ({}),
      })
    } else {
      that.showFaultInfo();
    }
  },
  //故障信息输入
  faultTypeInputlur: function (e) {
    var fault_type_input = e.detail.value;
    if (fault_type_input.length < 10 && fault_type_input.length > 0) {
      this.setData({
        fault_type_input: e.detail.value
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '其他故障详情有误，请重新输入',
      })
    }
  },
  //故障信息选择
  bindinfochange: function (e) {
    this.setData({
      info_index: e.detail.value,
    })

  },
  //订单提交
  gonext: function (e) {
    var that = this;
    var finalfaultinfo = "";

    if (that.data.fault_type[that.data.index] == "其他") {
      finalfaultinfo = that.data.fault_type_input;
    } else {
      finalfaultinfo = that.data.faultinfo[that.data.info_index]
    }
    if (that.data.tepvalue == "" && finalfaultinfo.length < 10 && finalfaultinfo.length > 0) {
    wx.request({
      url: that.data.url.add_order,
      data: {
        name:that.data.address.userName,
        faulttype: that.data.fault_type[that.data.index],
        faultinfo: finalfaultinfo,
        userId: app.data.user.id,
        region: that.data.address.provinceName + that.data.address.cityName + that.data.address.countyName + that.data.address.detailInfo,
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        that.setData({//如果在sucess直接写this就变成了wx.request()的this了.必须为getdata函数的this,不然无法重置调用函数
          orderId: res.data,//返回订单号
        })
        wx.showToast({
          title: '下单成功',
          icon: 'success',
          image: '',
          duration: 2000,
          mask: true,
          success: function (res) {
            wx.navigateTo({
              url: '../orderinfo/orderinfo?orderId=' + that.data.orderId,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })

          },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function (res) { },
      complete: function (res) {
      },
    })
    }
    else {
      wx.showToast({
        title: '订单信息不完整',
      })
    }
  },
  //选择省市区函数
  changeRegin(e) {
    this.setData({ region: e.detail.value });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    if(app.data.typeSelected) {
      that.setData({
        index_type: app.data.typeSelected
      })
    }
    that.showFaultInfo();
  },
  //获取故障类型
  getdata: function () {
    var that = this;
    wx.request({
      url: that.data.url.fault_type,
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        var index;
        var a = [];
        var json = res.data;
        for (var j in json) {
          a.push(json[j].type);
          if (that.data.index_type == json[j].type) {
            index = j;
          }
        }
        a.push("其他")
        that.setData({//如果在sucess直接写this就变成了wx.request()的this了.必须为getdata函数的this,不然无法重置调用函数
          fault_type: a,
        })
        that.showFaultInfo();
      },
      fail: function (res) { },
      complete: function (res) {
      },
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var list = that.data.list;
    var currentPage = that.data.currentPage;
    that.getdata();
    that.onLoad();
  },
  /**
   * 选择地址
   */
  choseAddress: function () {
    var that = this;
    wx.chooseAddress({
      success: function (res) {
        console.log("res",res)
        that.setData({
          address: res,
          region: [res.provinceName, res.cityName, res.countyName],
          tepvalue: "",
        })
      }
    })
  }
  
})
